﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    public enum STATE //定义枚举 状态
    {
        IDLE,//静止状态
        WALK,//走
        RUN,//跑
        JUMP,//跳
    }
    float _timer;//游戏中时间

    private float width = 200;//角色屏幕宽度
    private float height = 275;
    private Animator _animator;  //动画控制器
    //private bool OnGround; //角色在地面时
    //private bool Jump; //跳跃时

    public float Movespeed = 3f;  //走动速度
    public float Runspeed=10f;   //跑的速度
    //public float JumpForce;  //跳跃的力
    public STATE m_state;



    private void Awake()
    {
        _animator = this.GetComponent<Animator>();  //引用动画控制器
    }


    void Start () {
        m_state=STATE.IDLE; //开始时静止状态
    }
    
    void Update()
    {
        Move();
        _timer += Time.deltaTime;//时间
        
    }
    void Move()
    {
        Vector3 Position = Camera.main.WorldToScreenPoint(transform.position); //获取坐标
        #region   //人物移动
        if (Input.GetKey(KeyCode.RightArrow))  //键盘输入 右方向键
        {
            //Move(STATE.MOVERIGHT);
            m_state = STATE.WALK;
            if (Position.x < Screen.width - width / 2)
            {
                transform.Translate(Movespeed * Time.deltaTime, 0, 0);
            }
            this.gameObject.GetComponent<SpriteRenderer>().flipX = false; //动画X轴旋转关
        }
        else if (Input.GetKey(KeyCode.LeftArrow))  //键盘输入 左方向键
        {     
            m_state = STATE.WALK;
            if (Position.x > width / 2)
            {
                transform.Translate(-Movespeed * Time.deltaTime, 0, 0);
            }
            this.gameObject.GetComponent<SpriteRenderer>().flipX = true; //动画X轴旋转开
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            m_state = STATE.WALK;
            transform.Translate(0, Movespeed * Time.deltaTime, 0);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            m_state = STATE.WALK;
            transform.Translate(0, -Movespeed * Time.deltaTime, 0);
        }
        else
        {
            m_state = STATE.IDLE;
        }
        #endregion

        switch (m_state)
        {
            case STATE.IDLE:
                _animator.SetInteger("state", 0);
                break;
            case STATE.WALK:
                _animator.SetInteger("state", 1);
                break;
            case STATE.RUN:
                _animator.SetInteger("state", 2);
                break;
            case STATE.JUMP:
                break;
        }
    }
}
